package com.lzh.user.service.impl;

import com.lzh.article.entity.ApAuthor;
import com.lzh.article.feign.ApAuthorFeign;
import com.lzh.common.constants.BusinessConstants;
import com.lzh.common.pojo.Result;
import com.lzh.media.entity.WmUser;
import com.lzh.media.feign.MediaUserFeign;
import com.lzh.user.entity.ApUser;
import com.lzh.user.entity.ApUserRealname;
import com.lzh.user.mapper.ApUserMapper;
import com.lzh.user.mapper.ApUserRealnameMapper;
import com.lzh.user.service.ApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author lzh
 * @since 2021-06-24
 */
@Service
//@Slf4j
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

//    private static final Logger logger = (Logger) LoggerFactory.getLogger(ApUserRealnameServiceImpl.class);
    @Resource
    private ApUserRealnameMapper apUserRealnameMapper;
    @Resource
    private ApUserMapper apUserMapper;
    @Autowired
    private MediaUserFeign mediaUserFeign;
    @Autowired
    private ApAuthorFeign apAuthorFeign;
    /**
     * 审核通过
     * @param id ap_user_realname表 id
     * @return
     */
    @Override
    public boolean pass(Integer id) {
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(id);
        apUserRealname.setStatus(BusinessConstants.ApUserRealnameConstants.SHENHE_SUCESS);
        //修改认证表的认证状态
        Integer i = apUserRealnameMapper.updateById(apUserRealname);
        //查找用户
        ApUserRealname userRealname = apUserRealnameMapper.selectById(id);
        if (userRealname != null) {
            //查询需要审核认证的用户
            ApUser apUser = apUserMapper.selectById(userRealname.getUserId());
            Integer apUserId = apUser.getId();
            //调用media服务查找apuser对应的媒体号，判断是否存在
            WmUser wmUser = mediaUserFeign.getByUserId(apUserId);
            if (wmUser == null) {
                wmUser = new WmUser();
                //将数据copy到wmUser中
                BeanUtils.copyProperties(apUser,wmUser);
                //设置状态
                wmUser.setStatus(BusinessConstants.WmUserConstants.WM_USER_OK);
                //设置自媒体账号对应的用户id
                wmUser.setApUserId(apUser.getId());
                wmUser.setApAuthorId(apUser.getId());
                wmUser.setCreatedTime(LocalDateTime.now());
                Result result = mediaUserFeign.insert(wmUser);
                if (result.getCode() == 20000) {
                    System.out.println("自媒体账号创建成功");
                }
            }
            //调用ApAuthor微服务查找对应的作者，判断是否存在
            ApAuthor apAuthor = apAuthorFeign.getByUserId(apUserId);
            if (apAuthor == null) {
                apAuthor = new ApAuthor();
                //用户名就是作者名
                apAuthor.setName(apUser.getName());
                apAuthor.setType(BusinessConstants.ApAuthorConstants.A_MEDIA_USER);
                apAuthor.setUserId(apUser.getId());
                apAuthor.setId(apUser.getId());
                apAuthor.setWmUserId(wmUser.getId());
                apAuthor.setCreatedTime(LocalDateTime.now());
                Result result = apAuthorFeign.insert(apAuthor);
                if (result.getCode() == 20000) {
                    System.out.println("媒体人账号创建成功");
                }
            }
        }
        return i != null && i > 0;

    }

    /**
     * 审核驳回
     * @param id 审核id
     * @param reason 驳回原因
     * @return
     */
    @Override
    public boolean reject(Integer id, String reason) {
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(id);
        apUserRealname.setReason(reason);
        //设置状态为失败
        apUserRealname.setStatus(BusinessConstants.ApUserRealnameConstants.SHENHE_FAILE);
        Integer res = apUserRealnameMapper.updateById(apUserRealname);
        return res != null && res > 0;
    }

    //远程服务调用



}
